home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / tcclib.exe / IMWIN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-31  |  2.6 KB  |  107 lines

  1. #include <stdlib.h>
  2. #include <conio.h>
  3. #include <mem.h>
  4. #include "window.h"
  5. #include "_window.h"
  6.  
  7. static char *forimage;
  8.  
  9. void processimage(windowtype *window, char *image, unsigned inset,
  10.     int put)
  11. {
  12.     register unsigned t1,t2;
  13.     windowtype *current = window->previous;
  14.     unsigned top, bottom, left, right;
  15.     unsigned off1, off2, len, t;
  16.  
  17.     while ( current )
  18.     {
  19.         if (
  20.         (top    = ((t1=window->top+inset)    > (t2=current->top)    ? t1 : t2)) <=
  21.         (bottom = ((t1=window->bottom-inset) < (t2=current->bottom) ? t1 : t2)) &&
  22.         (left   = ((t1=window->left+inset)   > (t2=current->left)   ? t1 : t2)) <=
  23.         (right  = ((t1=window->right-inset)  < (t2=current->right)  ? t1 : t2)))
  24.           {
  25.             /* Update the forscreen and backscreen       */
  26.             /*   Calculate optimized index references */
  27.             off1 = ((window->right-inset) - (window->left+inset) + 1) << 1;
  28.             off2 = ((current->right) - (current->left) + 1) << 1;
  29.             len = (right - left + 1) << 1;
  30.  
  31.             /* calculate move positions */
  32.             t1 = (left - (window->left+inset)) << 1;
  33.             t2 = (left - (current->left)) << 1;
  34.  
  35.             if ( (window->top+inset) == top)
  36.                 t2 += off2*(top - (current->top));
  37.             else
  38.                 t1 += off1*(top - (window->top+inset));
  39.  
  40.             /*   Move the memory appropriately */
  41.             for (t=top ; t <= bottom; t++)
  42.             {
  43.                 if (put)
  44.                 {
  45.                     char *tmp = &image[t1];
  46.                     ifn0move(¤t->backbuffer[t2],tmp,len);
  47.                     memset(tmp,'\0',len);
  48.                 }
  49.                 else
  50.                     if0move(&image[t1],¤t->backbuffer[t2],len);
  51.                 t1 += off1;
  52.                 t2 += off2;
  53.             }
  54.         }
  55.         current = current->previous;
  56.     }
  57. }
  58.  
  59. char *getwinimage(windowtype *window, unsigned off)
  60. {
  61.     char *image;
  62.  
  63.     free(forimage);
  64.     forimage = NULL;
  65.  
  66.     if ( (image = calloc(window->size,1)) == NULL )
  67.         return NULL;
  68.  
  69.     if ( (forimage = malloc(window->size)) == NULL )
  70.     {
  71.         free(image);
  72.         return NULL;
  73.     }
  74.     processimage(window, image, off, 0);
  75.  
  76.     gettext(window->left+off, window->top+off, window->right-off,
  77.         window->bottom-off,forimage);
  78.     if0move(image, forimage, window->size);
  79.  
  80.     return image;
  81. }
  82.  
  83. int putwinimage(windowtype *window, char *image, unsigned off, int update)
  84. {
  85.     if ( !forimage )
  86.     {
  87.         if ( (forimage = malloc(window->size)) == NULL )
  88.             return -1;
  89.         gettext(window->left+off, window->top+off, window->right-off,
  90.             window->bottom-off,forimage);
  91.     }
  92.  
  93.     processimage(window, image, off, 1);
  94.  
  95.     ifn0move(forimage, image, window->size);
  96.  
  97.     if ( update )
  98.         puttext(window->left+off, window->top+off, window->right-off,
  99.             window->bottom-off,forimage);
  100.     else
  101.         memcpy(image, forimage, window->size);
  102.  
  103.     free(forimage);
  104.     forimage = NULL;
  105.  
  106.     return 0;
  107. }